<script>
	import Header from "@/components/Header.vue"
	export default {
		components: {
			Header
		},
		data() {
			return {
				payType: 0,
				title: "资源库",
				item: {
					resource_name: '',
					resource_age: "",
					resource_sex: "",
					resource_address: "",
					resource_address_two: "",
					resource_school: '',
					resource_school_two: '',
					resource_company: '',
					resource_company_two: ''
				},
				ziyuan_find: {},
				school: [],
				company: [],
				is_edit: 0,
				z_label_id: '',
				z_list: [],
				dataArr: {},

			}
		},
		onLoad(options) {
			console.log(options)
			if (options.item) {
				this.item = JSON.parse(options.item)
				console.log(this.item)
				this.is_edit = 1
				this.get_ziyuan(this.item.resource_id)
				this.editMyResource(this.item.resource_id)
			}

			var that = this
			uni.request({
				url: getApp().globalData.host + '/api/My/getSelectList',
				header: {
					memberid: uni.getStorageSync('member_id'),
					membertoken: uni.getStorageSync('member_token'),
				},
				success: (r) => {
					that.dataArr = r.data.data
					var lists = that.dataArr
					for (var i = 0; i < lists.list.length; i++) {
						if (lists.list[i].z_list.length > 0) {
							that.z_list = lists.list[i].z_list
							break
						}
					}
					that.z_list = lists.ziyuan_list
				},
			})

		},
		methods: {
			editMyResource(id) {
				var that = this
				uni.request({
					url: getApp().globalData.host + '/api/Resource/editMyResource',
					data: {
						edit_type: 1,
						resource_id: id
					},
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						if (r.data.code == 200) {
							that.item = r.data.data
							that.item.resource_id = r.data.data.id
							that.z_label_id = that.item.resource_label_id
							if (that.item.resource_school_two) {
								that.school.push({
									val: ''
								})
							}
							if (that.item.resource_company_two) {
								that.company.push({
									val: ''
								})
							}
						}
					},
				})
			},
			saveMyResource() {
				var that = this
				var item = that.item
				if (that.z_label_id == '') {
					uni.showToast({
						title: '请选择资源库类别',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (item.resource_name == '') {
					uni.showToast({
						title: '请输入资源库姓名',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (item.resource_sex == '') {
					uni.showToast({
						title: '请输入资源库性别',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}

				if (item.resource_age == '') {
					uni.showToast({
						title: '请输入资源库年龄',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				
				
				if ((that.item.resource_sex == '男')||(that.item.resource_sex == '1')) {
					item.resource_sex = 1
				}else if ((that.item.resource_sex == '女')||(that.item.resource_sex == '2')) {
					item.resource_sex = 2
				} else {
					item.resource_sex = 1
				}
	
				
				uni.request({
					url: getApp().globalData.host + '/api/Resource/editMyResource',
					data: {
						edit_type: 2,
						resource_id: item.resource_id,
						resource_name: item.resource_name,
						resource_age: item.resource_age,
						resource_sex: item.resource_sex,
						resource_address: item.resource_address,
						resource_address_two: item.resource_address_two,
						resource_school: item.resource_school,
						resource_school_two: item.resource_school_two,
						resource_company: item.resource_company,
						resource_company_two: item.resource_company_two,
						resource_label_id: that.z_label_id
					},
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						if (r.data.code == '200') {
							uni.showToast({
								title: '操作成功',
								icon: 'none',
								duration: 2000,
								success() {
									setTimeout(function() {
										uni.navigateBack({
											delta: 1,
										})
									}, 2000)

								}
							})
						} else {
							uni.showToast({
								title: '操作失败',
								icon: 'none',
								duration: 2000,
								success() {

								}
							})
						}
					},
				})
			},
			insert() {
				var that = this
				var item = that.item

				if (that.z_label_id == '') {
					uni.showToast({
						title: '请选择资源库类别',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (item.resource_name == '') {
					uni.showToast({
						title: '请输入资源库姓名',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}
				if (item.resource_sex == '') {
					uni.showToast({
						title: '请输入资源库性别',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}

				if (item.resource_age == '') {
					uni.showToast({
						title: '请输入资源库年龄',
						icon: 'none',
						duration: 2000,
						success() {}
					})
					return false;
				}



				if (that.item.resource_sex == '男') {
					item.resource_sex = 1
				} else if (that.item.resource_sex == '女') {
					item.resource_sex = 2
				} else {
					item.resource_sex = 1
				}

				uni.request({
					url: getApp().globalData.host + '/api/Resource/addMyResource',
					data: {
						resource_name: item.resource_name,
						resource_age: item.resource_age,
						resource_sex: item.resource_sex,
						resource_address: item.resource_address,
						resource_address_two: item.resource_address_two,
						
						resource_school: item.resource_school,
						resource_school_two: item.resource_school_two,
						resource_company: item.resource_company,
						resource_company_two: item.resource_company_two,
						resource_label_id: that.z_label_id
					},
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						if (r.data.code == '200') {
							uni.showToast({
								title: '操作成功',
								icon: 'none',
								duration: 2000,
								success() {
									setTimeout(function() {
										uni.navigateBack({
											delta: 1,
										})
									}, 2000)

								}
							})
						} else {
							uni.showToast({
								title: r.data.msg,
								icon: 'none',
								duration: 2000,
								success() {

								}
							})
						}

					},
				})
			},
			get_ziyuan(id) {
				var that = this
				uni.request({
					url: getApp().globalData.host + '/api/Resource/getResourceInfo',
					data: {
						resource_id: id
					},
					header: {
						memberid: uni.getStorageSync('member_id'),
						membertoken: uni.getStorageSync('member_token'),
					},
					success: (r) => {
						that.ziyuan_find = r.data.data
						console.log(that.ziyuan_find)
					},
				})
			},
			add(str) {
				if (str == 'school') {
					this.school.push({
						val: ''
					})
				} else {
					this.company.push({
						val: ''
					})
				}
				console.log(this.school);
			},
			yichu(item, index, str) {
				if (str == 'school') {
					this.school.splice(index, 1);
					this.item.resource_school_two = ''
				} else {
					this.company.splice(index, 1);
					this.item.resource_company_two = ''
				}
			},
			set_sex(i) {
				this.item.resource_sex = i
			},
			dianji(i) {
				this.z_label_id = i
			}
		}
	}
</script>
<template>
	<view class="custom-bg">
		<Header :title="title" />


		<!-- ======================表单 start ====================== -->
		<div class="bg-white mt-30 p-30" style="border-radius: 30rpx 30rpx 0 0 ;">
			<div class="flex flex-wrap between" style="margin-bottom: 50rpx;">
				<div class="fz30 text-gray-16 mt-37 mb-30">标签类别(单选)</div>
				<view style="width: 100%;" class="between flex flex-wrap ">
					<view @click="dianji(item.z_label_id)" class="brand s1 w-150 h-55"
						:class="z_label_id==item.z_label_id?'on':''" v-for="(item,ind) in z_list"><text
							style="font-size: 12px;">{{item.label_name}}</text></view>
				</view>
			</div>


			<div class="fz30 text-gray-16  mb-30">基本信息(必填)</div>

			<div class="flex">
				<div class="flex" @click="set_sex('男')">
					<image v-if="item.resource_sex=='男'||item.resource_sex=='1'" src="/static/publish/radios.png"
						mode="widthFix" class="w-30"></image>
					<image v-else src="/static/publish/radio.png" mode="widthFix" class="w-30"></image>
					<div class="fz30 text-gray-16 ml-11">男</div>
				</div>
				<div class="flex ml-50" @click="set_sex('女')">
					<image v-if="item.resource_sex=='女'||item.resource_sex=='2'" src="/static/publish/radios.png"
						mode="widthFix" class="w-30"></image>
					<image v-else src="/static/publish/radio.png" mode="widthFix" class="w-30"></image>
					<div class="fz30 text-gray-16 ml-11">女</div>
				</div>
			</div>

			<div class="custom-input mt-30">
				<div class="fz30 text-gray-16 mr-30 flex-shirnk-0">姓名</div>
				<input type="text" class="fz22 h-full w-full pt-5" placeholder="请输入对方姓名" v-model="item.resource_name">
			</div>

			<div class="custom-input mt-18">
				<div class="fz30 text-gray-16 mr-30 flex-shirnk-0">年龄</div>
				<input type="text" class="fz22 h-full w-full  pt-5" placeholder="请输入对方年龄" v-model="item.resource_age">
			</div>

			<div class="line"></div>


			<div class="fz30 text-gray-16 mt-37 mb-30">更多信息(选填)</div>

			<div class="custom-input mt-30">
				<div class="fz30 text-gray-16 mr-30 flex-shirnk-0">资源覆盖范围</div>
				<input type="text" class="fz22 h-full w-full  pt-5" placeholder="例如:全国/XX省/XX市/XX区"
					v-model="item.resource_address">
			</div>
			
			<div class="custom-input mt-30">
				<div class="fz30 text-gray-16 mr-30 flex-shirnk-0">资源覆盖方向</div>
				<input type="text" class="fz22 h-full w-full  pt-5" placeholder="例如:教育/医疗/人脉/等"
					v-model="item.resource_address_two">
			</div>

			<div class="between mt-18">
				<div class="custom-input">
					<div class="fz30 text-gray-16 mr-30 flex-shirnk-0">学校</div>
					<input type="text" class="fz22 h-full w-full  pt-5" placeholder="请输入对方学校名称"
						v-model="item.resource_school">
				</div>
				<div class="custom-input flex-col center w-135 ml-15" @click="add('school')">
					<image src="/static/publish/add.png" class="w-40" mode="widthFix"></image>
					<div class="fz24 text-gray-16">添加</div>
				</div>
			</div>
			<div class="between mt-18" v-show="school.length!=0">
				<div class="custom-input">
					<div class="fz30 text-gray-16 mr-30 flex-shirnk-0">学校</div>
					<input type="text" class="fz22 h-full w-full  pt-5" placeholder="请输入对方学校名称"
						v-model="item.resource_school_two">
				</div>
				<div class="custom-input flex-col center w-135 ml-15" @click="yichu(item,index,'school')">
					<image src="/static/publish/del.png" class="w-40" mode="widthFix"></image>
					<div class="fz24 text-gray-16">移除</div>
				</div>
			</div>
			<div class="between mt-18">
				<div class="custom-input">
					<div class="fz30 text-gray-16 mr-30 flex-shirnk-0">公司</div>
					<input type="text" class="fz22 h-full w-full  pt-5" placeholder="请输入对方所任职公司名称"
						v-model="item.resource_company">
				</div>
				<div class="custom-input flex-col center w-135 ml-15" @click="add('company')">
					<image src="/static/publish/add.png" class="w-40" mode="widthFix"></image>
					<div class="fz24 text-gray-16">添加</div>
				</div>
			</div>
			<div class="between mt-18" v-show="company.length!=0">
				<div class="custom-input">
					<div class="fz30 text-gray-16 mr-30 flex-shirnk-0">公司</div>
					<input type="text" class="fz22 h-full w-full  pt-5" placeholder="请输入对方所任职公司名称"
						v-model="item.resource_company_two">
				</div>
				<div class="custom-input flex-col center w-135 ml-15" @click="yichu(item,index,'company')">
					<image src="/static/publish/del.png" class="w-40" mode="widthFix"></image>
					<div class="fz24 text-gray-16">移除</div>
				</div>
			</div>
		</div>
		<!-- ====================== 表单 end ====================== -->

		<div class="submit  text-white p-30 bg-white between px-50">
			<div class="bg-purple center h-90 rounded-50 w-full" @click="insert()" v-if="is_edit==0">
				保存
			</div>
			<div class="bg-purple center h-90 rounded-50 w-full" @click="saveMyResource()" v-if="is_edit==1">
				保存
			</div>
		</div>

		<div style="height: 300rpx;"></div>

	</view>
</template>



<style>
	.submit {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 3;
	}

	page {
		background-color: #F8F8F8;
	}

	,
	.brand {
		background-color: #f5f5f5;
		color: #000;
		border-radius:10rpx;
		line-height: 55rpx;
		text-align: center;
		margin-top: 10px;
	}

	.on {
		background-color: #AC5FF8 !important;
		color: #fff;
	}
</style>